//
//  ReviewPhotosController.swift
//  QualityControlSystem
//
//  Created by dundun on 17/1/13.
//  Copyright © 2017年 com.mask. All rights reserved.
//

import UIKit
import SnapKit

class ReviewPhotosController: UIViewController {
    
    var reviewType: ReviewType?
    var workerType: WorkerType?
    
    var isShow: Bool = false
    var showHight:CGFloat = 44.0
    var dataArr: [String]?
    var commitReviewBtn: UIButton = UIButton(type: UIButtonType.system)
    var reviewCollectionView: UICollectionView?
    var header: UILabel?
    
    var testText: String = "卡哈斯卡大客户是打不死的骄傲SD卡缴纳了卡萨诺大懒懒地看那看神经内科节哀顺变课教案上课都能看到卡拉上你的"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        automaticallyAdjustsScrollViewInsets = false;
        if reviewType != ReviewType.reviewFailure && workerType == WorkerType.checker {
            setupRightBarButtonItems()
        }
        setupHeader()
        setupCollectionView()
        setupCommitReviewBtn()
        loadData()
    }
    
    // MARK: - right button relate
    
    func setupRightBarButtonItems() {
//        let rightBtn = UIButton(type: UIButtonType.system)
//        rightBtn.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
//        rightBtn.setTitle("历史记录", for: .normal)
//        rightBtn.setTitleColor(UIColor.black, for: .normal)
//        rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
//        rightBtn.titleLabel?.layer.cornerRadius = 10.0
//        rightBtn.titleLabel?.layer.masksToBounds = true
//        rightBtn.titleLabel?.layer.borderWidth = 1.0
//        rightBtn.titleLabel?.layer.borderColor = UIColor.lightGray.cgColor
//        rightBtn.addTarget(self, action: #selector(rightBtnClicked(rightBtn:)), for: .touchUpInside)
//        let rightBarBtn = UIBarButtonItem(customView: rightBtn)
        let rightBarBtn = UIBarButtonItem.init(title: "历史记录", style: .plain, target: self, action: #selector(rightBtnClicked(rightBtn:)))
        navigationItem.rightBarButtonItem = rightBarBtn
    }
    
    func rightBtnClicked(rightBtn: UIBarButtonItem) {
        let historyPicListVC = HistoryPicListController(style: .grouped, processId: "")
        navigationController?.pushViewController(historyPicListVC, animated: true)
    }
    
    func setupCommitReviewBtn() {
        view.addSubview(commitReviewBtn)
        commitReviewBtn.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left)
            make.centerX.equalTo(view.snp.centerX)
            make.height.equalTo(44.0)
            make.bottom.equalTo(view.snp.bottom)
        }
        
        if reviewType == ReviewType.reviewSuccess {
            commitReviewBtn.backgroundColor = UIColor.green
            commitReviewBtn.setTitle("审核已通过", for: .normal)
            commitReviewBtn.setTitleColor(UIColor.white, for: .normal)
            commitReviewBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        } else {
            commitReviewBtn.layer.cornerRadius = 5
            commitReviewBtn.layer.masksToBounds = true
            commitReviewBtn.layer.borderColor = UIColor.lightGray.cgColor
            commitReviewBtn.layer.borderWidth = 1.0
            commitReviewBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
            commitReviewBtn.setTitleColor(UIColor.black, for: .normal)
            if reviewType == ReviewType.reviewWaiting {
                commitReviewBtn.setTitle("提交质检报告", for: .normal)
            } else {
                commitReviewBtn.setTitle("提交审核申请", for: .normal)
            }
        }
    }
    
    func setupCollectionView() {
        let layout = UICollectionViewFlowLayout()
        if reviewType == ReviewType.reviewFailure {
            reviewCollectionView = UICollectionView.init(frame: CGRect(x: 0, y: 44, width: Screen.width, height: Screen.height - 44 - 64 - 44), collectionViewLayout: layout)
        } else {
            reviewCollectionView = UICollectionView.init(frame: CGRect(x: 0, y: 0, width: Screen.width, height: Screen.height - 64 - 44), collectionViewLayout: layout)
        }
        reviewCollectionView?.backgroundColor = UIColor.white
        reviewCollectionView?.alwaysBounceVertical = true
        reviewCollectionView?.delegate = self
        reviewCollectionView?.dataSource = self
        reviewCollectionView?.register(PhotoCell.self, forCellWithReuseIdentifier: "photoCell")
        view.addSubview(reviewCollectionView!
        
        )
    }
    
    func setupHeader() {
        if reviewType == ReviewType.reviewFailure {
            header = UILabel()
            header?.isUserInteractionEnabled = true
            header?.backgroundColor = UIColor.red
            header?.frame = CGRect(x: 0, y: 0, width: Screen.width, height: 44)
            header?.textColor = UIColor.white
            header?.text = testText
            header?.font = UIFont.systemFont(ofSize: 14.0)
            header?.numberOfLines = 1;
            let gesture = UITapGestureRecognizer(target: self, action: #selector(headerViewClicked(gesture:)))
            header?.addGestureRecognizer(gesture)
            view.addSubview(header!)
        }
    }
    
    func headerViewClicked(gesture: UITapGestureRecognizer) {
        if isShow {
            header?.numberOfLines = 1
            showHight = 44.0
            header?.frame = CGRect(x: 0, y: 0, width: Screen.width, height: 44)
            isShow = false
        } else {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineBreakMode = .byCharWrapping;
            let attributes = [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 14.0), NSParagraphStyleAttributeName:paragraphStyle.copy] as [String : Any]
            let labelSize = testText.boundingRect(with: CGSize(width: Screen.width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes:attributes, context: nil)
            
            header?.numberOfLines = 0
            showHight = labelSize.size.height
            header?.frame = CGRect(x: 0, y: 0, width: labelSize.width, height: labelSize.height)
            isShow = true
        }
        reviewCollectionView?.frame = CGRect(x: 0, y: (header?.frame.maxY)!, width: Screen.width, height: 44)
    }
    
    func loadData() {
        dataArr = ["1", "2", "3"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ReviewPhotosController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    // MARK: - UICollectionViewDataSource
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if reviewType == ReviewType.reviewWaiting {
            return dataArr?.count ?? 0
        } else {
            return (dataArr?.count ?? 0) + 1
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let photoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? PhotoCell
        if reviewType == ReviewType.reviewFailure {
            if indexPath.row == collectionView.numberOfSections - 1 {
//                photoCell?.photo?.image = UIImage(named: "固定图片")
            } else {
//                photoCell?.photo?.image = UIImage(named: (dataArr?[indexPath.row])!)
            }
        } else {
//            photoCell?.photo?.image = UIImage(named: (dataArr?[indexPath.row])!)
        }
        return photoCell!
    }

    
    // MARK: - UICollectionViewDelegateFlowLayout
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: Screen.width/2 - 2, height: Screen.width/2 - 2)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10.0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 2.0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
    }
    
}
